package com.wish.business.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wish.business.entity.Business;
import com.wish.business.entity.vo.BusinessVo;
import com.wish.business.enums.BusinessType;
import com.wish.business.service.IBusinessService;
import com.wish.common.core.domain.R;
import com.wish.common.core.usconfig.common.MsgResult;
import com.wish.common.security.annotation.RequiresPermissions;
import com.wish.system.api.RemoteFileService;
import com.wish.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.MultipartProperties;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 杨子强-商户表 前端控制器
 * </p>
 *
 * @author benjamin
 * @since 2022-03-26
 */
@RestController
@RequestMapping("/business")
public class BusinessController {

    @Autowired
    IBusinessService iBusinessService;

    @GetMapping("/businessList")
    @RequiresPermissions("system:business:index")
    public MsgResult businessList(BusinessVo vo) {
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        List<BusinessVo> list = iBusinessService.businessList();
        PageInfo<BusinessVo> pg = new PageInfo<>(list);
        return MsgResult.succeed(pg);
    }

    @GetMapping("/getBusMsg/{id}")
    public MsgResult getBusMsg(@PathVariable("id") Long id) {
        BusinessVo one = iBusinessService.getBusMsg(id);
        return MsgResult.succeed(one);
    }

    @PostMapping("/check")
    public MsgResult check(@RequestBody BusinessVo businessVo) {
        int business = iBusinessService.updateStatus(businessVo);
        return MsgResult.succeed(business);
    }

    @GetMapping("/typeList")
    public MsgResult typeList() {
        BusinessType[] values = BusinessType.values();
        return MsgResult.succeed(values);
    }

    //    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Resource
    RemoteFileService remoteFileService;

    @PostMapping("/saveOrUpdateBusiness")
    public MsgResult saveOrUpdateBusiness(@RequestBody Business business) {
        business.setCreateTime(new Date());
        iBusinessService.saveOrUpdate(business);
        return MsgResult.succeed();
    }

    @PostMapping("/uploadHead")
    public MsgResult uploadHead(@RequestParam("file") MultipartFile file) {
        /*String imgs = "";
        for (int i = 0; i < files.length; i++) {
            R<SysFile> upload = remoteFileService.upload(files[i]);
            String url = upload.getData().getUrl();
            imgs += "," + url;
        }*/
        R<SysFile> upload = remoteFileService.upload(file);
        String replace = upload.getData().getUrl().replace("47.94.105.85:9001", "47.94.105.85:9000");
        return MsgResult.succeed(replace);
    }

}
